package com.weim.schedule;

import com.weim.utils.CommonUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author weim
 * @date 18-8-20
 */
@Component
public class GetCookie {

    //@Scheduled(cron = "0 0 0/1 * * ?")
    public void getCookie() {
        try {
            URL url = new URL("http://www.biquge.com.tw/");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);

            if(connection.getResponseCode() == 521) {
                String setCookie = connection.getHeaderField("Set-Cookie");
                CommonUtils.cookie = setCookie.substring(0,setCookie.indexOf(";"));
            }
        } catch (Exception e) {
            System.out.println(" 获取 cookie 错误 ==>" + e.getMessage());
        }
    }


}
